﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jQueryDotNetLib.Controls
{
    [ToolboxData("<{0}:Slider runat=server />")]
    public class Slider : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Values
        {
            get
            {
                if (ViewState["Values"] == null) return "";
                return (string)ViewState["Values"];
            }

            set
            {
                ViewState["Values"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public double Value
        {
            get
            {
                if (ViewState["Value"] == null) return 0;
                return (double)ViewState["Value"];
            }
            set
            {
                ViewState["Value"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Animate
        {
            get
            {
                if (ViewState["Animate"] == null) return false;
                return (bool)ViewState["Animate"];
            }
            set
            {
                ViewState["Animate"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public double Max
        {
            get
            {
                if (ViewState["Max"] == null) return 100;
                return (double)ViewState["Max"];
            }
            set
            {
                ViewState["Max"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public double Min
        {
            get
            {
                if (ViewState["Min"] == null) return 0;
                return (double)ViewState["Min"];
            }
            set
            {
                ViewState["Min"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Orientation
        {
            get
            {
                if (ViewState["Orientation"] == null) return "auto";
                return (string)ViewState["Orientation"];
            }
            set
            {
                ViewState["Orientation"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Range
        {
            get
            {
                if (ViewState["Range"] == null) return "false";
                return (string)ViewState["Range"];
            }
            set
            {
                ViewState["Range"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Step
        {
            get
            {
                if (ViewState["Step"] == null) return 1;
                return (int)ViewState["Step"];
            }
            set
            {
                ViewState["Step"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientChange
        {
            get
            {
                if (ViewState["OnClientChange"] == null) return "";
                return (string)ViewState["OnClientChange"];
            }

            set
            {
                ViewState["OnClientChange"] = value;
            }
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientStart
        {
            get
            {
                if (ViewState["OnClientStart"] == null) return "";
                return (string)ViewState["OnClientStart"];
            }

            set
            {
                ViewState["OnClientStart"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientSlide
        {
            get
            {
                if (ViewState["OnClientSlide"] == null) return "";
                return (string)ViewState["OnClientSlide"];
            }

            set
            {
                ViewState["OnClientSlide"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientStop
        {
            get
            {
                if (ViewState["OnClientStop"] == null) return "";
                return (string)ViewState["OnClientStop"];
            }

            set
            {
                ViewState["OnClientStop"] = value;
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {

            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<div id=" + this.ClientID + "></div>");

            sb.Append("<script type=\"text/javascript\">");
            sb.Append("jQuery(document).ready(function($) { var ctl = $(\"#" + this.ClientID + "\"); ctl.slider( { ");

            bool needsComma = false;
            if (OnClientStart != "")
            {
                sb.Append("start: function(event, ui) {" + OnClientStart + "}");
                needsComma = true;
            }
            if (OnClientSlide != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("slide: function(event, ui) {" + OnClientSlide + "}");
                needsComma = true;
            }
            if (OnClientChange != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("change: function(event, ui) {" + OnClientChange + "}");
                needsComma = true;
            }
            if (OnClientStop != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("stop: function(event, ui) {" + OnClientStop + "}");
                needsComma = true;
            }

            if (Value > 0)
            {
                if (needsComma) sb.Append(",");
                sb.Append("value: " + Value);
                needsComma = true;
            }
            if (Values != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("values: " + Values);
                needsComma = true;
            }
            if (Animate)
            {
                if (needsComma) sb.Append(",");
                sb.Append("animate: true");
                needsComma = true;
            }
            if (Max != 100)
            {
                if (needsComma) sb.Append(",");
                sb.Append("max: " + Max);
                needsComma = true;
            }
            if (Min != 0)
            {
                if (needsComma) sb.Append(",");
                sb.Append("min: " + Min);
                needsComma = true;
            }
            if (Orientation != "auto")
            {
                if (needsComma) sb.Append(",");
                sb.Append("orientation: '" + Orientation);
                needsComma = true;
            }
            if (Range != "false")
            {
                if (needsComma) sb.Append(",");
                sb.Append("range: " + Range);
                needsComma = true;
            }
            if (Step != 1)
            {
                if (needsComma) sb.Append(",");
                sb.Append("step: " + Step);
                needsComma = true;
            }


            sb.Append("} ); ");

            sb.Append("});");
            sb.Append("</script>");

            writer.Write(sb.ToString());

        }

    }
}